package shop.controller;


import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.flown.shop.entity.error.ReadError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.entity.GoodsComment;
import shop.service.GoodsCommentService;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Base64;


@RequestMapping("/comment/goods")
@Controller
public class GoodsCommentController
{
    @Autowired
    private GoodsCommentService service_goods_comment;


    @ResponseBody
    @RequestMapping("/getGoodsCommentByID")
    public GoodsComment getGoodsCommentByID(@RequestParam("id") int comment_id)
    {
        return this.service_goods_comment.getGoodsCommentByID(comment_id);
    }


    @RequestMapping("/getGoodsCommentPictureByIDAndIndex")
    public void getGoodsCommentPictureByIDAndIndex(HttpServletResponse response,
                                                   @RequestParam("id") int comment_id,
                                                   @RequestParam("index") int picture_index)
            throws IOException
    {
        GoodsComment comment = this.getGoodsCommentByID(comment_id);
        if (comment == null)
        {
            PrintWriter out = response.getWriter();
            out.println("评论不存在");
            out.close();
            return;
        }
        byte[] bytes;
        try {
            bytes = comment.getPicture(picture_index);
            String format = comment.getPictureFormat(picture_index);

            switch (format) {
                case "jpg", "jpeg" -> response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                case "png" -> response.setContentType(MediaType.IMAGE_PNG_VALUE);
                case "gif" -> response.setContentType(MediaType.IMAGE_GIF_VALUE);
                default -> throw new ReadError("unknown picture format");
            }

            OutputStream out = response.getOutputStream();
            out.write(bytes);
            out.close();
        } catch (ReadError e) {
            PrintWriter out = response.getWriter();
            out.println("图片读取错误");
            out.close();
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }


    /**
     * @return 返回base64编码后的图片
     * */
    @ResponseBody
    @RequestMapping("/getGoodsCommentPicturesByID")
    public String[] getGoodsCommentPicturesByID(@RequestParam("id") int comment_id)
            throws ReadError, IOException
    {
        GoodsComment comment = this.getGoodsCommentByID(comment_id);
        if (comment == null)
            return null;
        byte[][] pictures = comment.getPictures();
        String[] base64_pictures = new String[pictures.length];
        for (int i = 0; i < pictures.length; i++)
            base64_pictures[i] = Base64.getEncoder().encodeToString(pictures[i]);
        return base64_pictures;
    }


    /**
     * goodsID
     * userID
     * storeID
     * ordersID
     * detailStar 默认5
     * logisticsStar 默认5
     * attitudeStar 默认5
     * type 默认GoodsComment.Type.NORMAL，不需要
     * content 可空
     * base64Picture 可空
     * */
    public void insertGoodsComment(@Valid GoodsComment comment) {this.service_goods_comment.insertGoodsComment(comment);}
    public void deleteGoodsCommentByID(int id) {this.service_goods_comment.deleteGoodsCommentByID(id);}




}




